package xyz.yuzegod.megawalls.classes.novice.zombie;

import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.Skill;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.ParticleEffect;
import xyz.yuzegod.megawalls.util.PlayerUtils;

import java.util.ArrayList;
import java.util.List;

public class MainSkill extends Skill
{
    public MainSkill(final Classes classes) {
        super("治愈之环", classes);
    }
    
    @Override
    public int maxedLevel() {
        return 5;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 4.2;
            }
            case 2: {
                return 4.8;
            }
            case 3: {
                return 5.4;
            }
            case 4: {
                return 6.0;
            }
            case 5: {
                return 6.6;
            }
            default: {
                return 4.2;
            }
        }
    }
    
    public double getMateAttribute(final int level) {
        switch (level) {
            case 1: {
                return 3.5;
            }
            case 2: {
                return 3.9;
            }
            case 3: {
                return 4.3;
            }
            case 4: {
                return 4.7;
            }
            case 5: {
                return 5.1;
            }
            default: {
                return 3.55;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        if (level == 1) {
            lore.add(" §8▪ §7恢复你自己 §a" + this.getAttribute(level) + "血量");
            lore.add("   §7和你附近的队友 §a" + this.getMateAttribute(level) + "血量");
            return lore;
        }
        lore.add(" §8▪ §7恢复你自己 §8" + this.getAttribute(level - 1) + " ➜ §a" + this.getAttribute(level) + "血量");
        lore.add("   §7和你附近的队友 §8" + this.getMateAttribute(level - 1) + " ➜ §a" + this.getMateAttribute(level) + "血量");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkillLevel();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkillLevel();
    }
    
    @Override
    public boolean use(final GamePlayer gamePlayer, final KitStatsContainer kitStats) {
        final int level = kitStats.getSkillLevel();
        final Player player = gamePlayer.getPlayer();
        PlayerUtils.heal(player, this.getMateAttribute(level));
        int counter = 0;
        for (final Player teammate : this.getTeammates(player, 5)) {
            PlayerUtils.heal(teammate, this.getMateAttribute(level));
            ++counter;
            teammate.sendMessage("§a§l你被" + player.getDisplayName() + "的治愈之环技能治疗了");
        }
        gamePlayer.sendMessage("§1§l你和§a§l" + counter + "§a§l名队友§1§l的治愈之环技能治疗了！");
        new BukkitRunnable() {
            int step = 0;
            
            public void run() {
                ++this.step;
                if (this.step == 40 || !gamePlayer.isOnline()) {
                    this.cancel();
                    return;
                }
                ParticleEffect.HEART.display(0.2f, 0.5f, 0.2f, 1.0f, 1, gamePlayer.getPlayer().getLocation().add(0.0, 1.0, 0.0), 10.0);
            }
        }.runTaskTimer((Plugin)MegaWalls.getInstance(), 0L, 1L);
        return true;
    }
    
    private List<Player> getTeammates(final Player player, final int radius) {
        final List<Player> players = new ArrayList<Player>();
        for (final Player other : PlayerUtils.getNearbyPlayers((Entity)player, radius)) {
            final GamePlayer gameOther = GamePlayer.get(other.getUniqueId());
            if (!gameOther.isSpectator()) {
                if (!GamePlayer.get(player.getUniqueId()).getGameTeam().isInTeam(gameOther)) {
                    continue;
                }
                players.add(other);
            }
        }
        return players;
    }
}
